package com.shell.governor.security.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.governor.modules.po.PubDictionary;
import com.shell.governor.modules.service.PubDictionaryService;
import com.shell.governor.security.user.po.PubSecurityRule;
import com.shell.governor.security.user.service.PubSecurityService;
import com.shell.governor.user.dao.PubUserDao;
import com.shell.governor.user.vo.PubUserVo;
import com.shell.governor.user.vo.UserQueryVo;

import tk.mybatis.mapper.entity.Example;

@RestController
public class PubSecurityController extends BaseController {
	@Autowired
	private PubSecurityService pubSecurityService;
	@Autowired
	private PubDictionaryService pubDictionaryService;
	@Autowired
	private PubUserDao userDao;

	@RequestMapping
	@Logging(module = "������������", methods = "����������������")
	public List<PubSecurityRule> selectPubSecurityRules() throws Exception {
		return this.pubSecurityService.selectSecurityRules();
	}

	@RequestMapping
	@Logging(module = "������������", methods = "������������")
	public RetMsg updatePubSecurityRule(PubSecurityRule PubSecurityRule, HttpServletRequest request) throws Exception {
		return this.pubSecurityService.updateSecurityRule(PubSecurityRule);
	}

	@RequestMapping({ "auth/rule" })
	@Logging(module = "��������", methods = " ����������������")
	public PubDictionary getPubSecurityRule() {
		PubDictionary dict = new PubDictionary();
		dict.setDicCode("rule_strength");
		dict = (PubDictionary) this.pubDictionaryService.selectOne(dict);
		Example example = new Example(PubDictionary.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("parentId", dict.getDicId());
		example.setOrderByClause("dic_sort asc");
		dict.setChildren(this.pubDictionaryService.selectByExample(example));
		return dict;
	}

	@RequestMapping({ "auth/getLoginSecurityRule" })
	@Logging(module = "��������", methods = " ����������������������")
	public PubSecurityRule getLoginSecurityRule() {
		return this.pubSecurityService.getLoginSecurityRule();
	}

	@RequestMapping({ "auth/check-phone-exist" })
	@Logging(module = "��������", methods = "������������������")
	public boolean checkPhoneExist(String phoneNum) {
		if (StringUtil.empty(phoneNum)) {
			return false;
		}
		UserQueryVo userVo = new UserQueryVo();
		userVo.setPhoneNo(phoneNum);
		userVo.setUserState("E");
		List<PubUserVo> users = this.userDao.queryUser(userVo);
		if ((users == null) || (users.size() == 0)) {
			return false;
		}
		return true;
	}
}
